/**
 * 仪表盘页面JavaScript功能
 */
$(function() {
    // 页面加载完成后执行
    initDashboard();
});

/**
 * 初始化仪表盘
 */
function initDashboard() {
    // 加载统计数据
    loadStatistics();
    
    // 加载图表
    loadCharts();
    
    // 加载最近活动
    loadRecentActivities();
}

/**
 * 加载统计数据
 */
function loadStatistics() {
    // 这里可以通过AJAX请求后端数据
    // 示例使用模拟数据
    updateStatistics({
        warehouseCount: 5,
        productCount: 120,
        orderCount: 35,
        alertCount: 3
    });
}

/**
 * 更新统计数据显示
 */
function updateStatistics(data) {
    $('#warehouseCount').text(data.warehouseCount || 0);
    $('#productCount').text(data.productCount || 0);
    $('#orderCount').text(data.orderCount || 0);
    $('#alertCount').text(data.alertCount || 0);
}

/**
 * 加载图表数据
 */
function loadCharts() {
    // 加载订单统计图表
    renderOrderChart();
    
    // 加载产品分布图表
    renderProductChart();
}

/**
 * 渲染订单统计图表
 */
function renderOrderChart() {
    var ctx = document.getElementById('orderChart');
    if (!ctx) return;
    
    ctx = ctx.getContext('2d');
    
    // 模拟数据
    var data = {
        labels: ['一月', '二月', '三月', '四月', '五月', '六月'],
        datasets: [
            {
                label: '订单数量',
                backgroundColor: 'rgba(78, 115, 223, 0.5)',
                borderColor: 'rgba(78, 115, 223, 1)',
                borderWidth: 1,
                data: [15, 22, 18, 24, 33, 28]
            },
            {
                label: '订单金额(千元)',
                backgroundColor: 'rgba(28, 200, 138, 0.5)',
                borderColor: 'rgba(28, 200, 138, 1)',
                borderWidth: 1,
                data: [25, 32, 28, 36, 45, 52]
            }
        ]
    };
    
    new Chart(ctx, {
        type: 'bar',
        data: data,
        options: {
            responsive: true,
            maintainAspectRatio: false,
            scales: {
                yAxes: [{
                    ticks: {
                        beginAtZero: true
                    }
                }]
            }
        }
    });
}

/**
 * 渲染产品分布图表
 */
function renderProductChart() {
    var ctx = document.getElementById('productChart');
    if (!ctx) return;
    
    ctx = ctx.getContext('2d');
    
    // 模拟数据
    var data = {
        labels: ['谷物', '蔬菜', '水果', '肉类', '水产', '乳制品'],
        datasets: [{
            data: [25, 20, 15, 18, 12, 10],
            backgroundColor: [
                '#4e73df',
                '#1cc88a',
                '#36b9cc',
                '#f6c23e',
                '#e74a3b',
                '#6f42c1'
            ]
        }]
    };
    
    new Chart(ctx, {
        type: 'pie',
        data: data,
        options: {
            responsive: true,
            maintainAspectRatio: false
        }
    });
}

/**
 * 加载最近活动
 */
function loadRecentActivities() {
    // 加载最近订单
    loadRecentOrders();
    
    // 加载库存预警
    loadStockAlerts();
}

/**
 * 加载最近订单
 */
function loadRecentOrders() {
    // 模拟数据
    var data = [
        { orderNo: 'O202305001', customerName: '张三', totalAmount: 2500, orderStatus: 'completed', createTime: '2023-05-15 14:30:00' },
        { orderNo: 'O202305002', customerName: '李四', totalAmount: 1800, orderStatus: 'processing', createTime: '2023-05-16 10:15:00' },
        { orderNo: 'O202305003', customerName: '王五', totalAmount: 3200, orderStatus: 'pending', createTime: '2023-05-17 09:45:00' }
    ];
    
    renderRecentOrders(data);
}

/**
 * 渲染最近订单
 */
function renderRecentOrders(data) {
    if (!data || data.length === 0) return;
    
    var html = '';
    
    data.forEach(function(item) {
        // 设置订单状态样式和文本
        var statusClass = '';
        var statusText = '';
        
        switch (item.orderStatus) {
            case 'pending':
                statusClass = 'badge badge-primary';
                statusText = '待处理';
                break;
            case 'processing':
                statusClass = 'badge badge-warning';
                statusText = '处理中';
                break;
            case 'completed':
                statusClass = 'badge badge-success';
                statusText = '已完成';
                break;
            case 'cancelled':
                statusClass = 'badge badge-danger';
                statusText = '已取消';
                break;
            default:
                statusClass = 'badge badge-secondary';
                statusText = '未知';
        }
        
        html += '<tr>' +
            '<td>' + item.orderNo + '</td>' +
            '<td>' + item.customerName + '</td>' +
            '<td>¥' + parseFloat(item.totalAmount).toFixed(2) + '</td>' +
            '<td><span class="' + statusClass + '">' + statusText + '</span></td>' +
            '<td>' + item.createTime + '</td>' +
        '</tr>';
    });
    
    $('#recentOrders').html(html);
}

/**
 * 加载库存预警
 */
function loadStockAlerts() {
    // 模拟数据
    var data = [
        { productName: '大米', warehouseName: '1号仓', quantity: 5, unit: 't', status: 'low' },
        { productName: '苹果', warehouseName: '冷藏仓', quantity: 3, unit: 't', status: 'low' },
        { productName: '牛肉', warehouseName: '冷冻仓', quantity: 0, unit: 't', status: 'empty' }
    ];
    
    renderStockAlerts(data);
}

/**
 * 渲染库存预警
 */
function renderStockAlerts(data) {
    if (!data || data.length === 0) return;
    
    var html = '';
    
    data.forEach(function(item) {
        // 设置状态样式和文本
        var statusClass = '';
        var statusText = '';
        
        switch (item.status) {
            case 'low':
                statusClass = 'badge badge-warning';
                statusText = '库存不足';
                break;
            case 'expiring':
                statusClass = 'badge badge-danger';
                statusText = '即将过期';
                break;
            case 'empty':
                statusClass = 'badge badge-danger';
                statusText = '库存耗尽';
                break;
            default:
                statusClass = 'badge badge-secondary';
                statusText = '未知';
        }
        
        html += '<tr>' +
            '<td>' + item.productName + '</td>' +
            '<td>' + item.warehouseName + '</td>' +
            '<td>' + item.quantity + ' ' + item.unit + '</td>' +
            '<td><span class="' + statusClass + '">' + statusText + '</span></td>' +
        '</tr>';
    });
    
    $('#stockAlerts').html(html);
} 